package io.github.wanggit.antrpc.commons.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class ArrayClassNameUtil {
    private ArrayClassNameUtil() {
        throw new IllegalStateException();
    }

    private static final Map<String, String> ARRAY_CLASS_NAMES;
    private static final Map<String, String> JAVA_ARRAY_NAME_TO_HUMAN_NAMES;

    static {
        Map<String, String> m = new HashMap<>();
        m.put("int[]", "[I");
        m.put("boolean[]", "[Z");
        m.put("float[]", "[F");
        m.put("long[]", "[J");
        m.put("short[]", "[S");
        m.put("byte[]", "[B");
        m.put("double[]", "[D");
        m.put("char[]", "[C");
        ARRAY_CLASS_NAMES = Collections.unmodifiableMap(m);

        Map<String, String> m2 = new HashMap<>();
        m2.put("[I", "int[]");
        m2.put("[Z", "boolean[]");
        m2.put("[F", "float[]");
        m2.put("[J", "long[]");
        m2.put("[S", "short[]");
        m2.put("[B", "byte[]");
        m2.put("[D", "double[]");
        m2.put("[C", "char[]");
        JAVA_ARRAY_NAME_TO_HUMAN_NAMES = Collections.unmodifiableMap(m2);
    }

    public static String replaceArrayClassName(String fullMethodName) {
        for (Map.Entry<String, String> entry : ARRAY_CLASS_NAMES.entrySet()) {
            fullMethodName = StringUtils.replace(fullMethodName, entry.getKey(), entry.getValue());
        }
        fullMethodName = fullMethodName.replaceAll("([\\w._$]+)\\[\\]", "[L$1;");
        return fullMethodName;
    }

    /**
     * @param type java array name [I
     * @return int[]
     */
    public static String javaArrayNameToHumanName(String type) {
        String result = JAVA_ARRAY_NAME_TO_HUMAN_NAMES.get(type.trim());
        if (null != result) {
            return result;
        }
        // [Ljava.lang.Integer; --> java.lang.Integer[]
        if (type.trim().matches("^\\[L[\\w._$]+;$")) {
            return type.substring(2, type.length() - 1) + "[]";
        }
        return type;
    }

    /**
     * 把所有 [Ljava.lang.Integer; --> java.lang.Integer[] 做转换
     *
     * @param content 文本
     * @return 转为可读性高的文本
     */
    public static String allJavaArrayNameToHumanName(String content) {
        if (null == content) {
            return null;
        }
        content = content.trim();
        for (Map.Entry<String, String> entry : JAVA_ARRAY_NAME_TO_HUMAN_NAMES.entrySet()) {
            content = content.replace(entry.getKey(), entry.getValue());
        }
        Pattern pattern = Pattern.compile("(\\[L([\\w._$]+);)");
        Matcher matcher = pattern.matcher(content);
        int start = 0;
        while (matcher.find(start)) {
            start = matcher.end();
            String target = matcher.group(1);
            String replacement = matcher.group(2);
            content = content.replace(target, replacement + "[]");
        }
        return content;
    }
}
